package redis;


import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.*;

import java.util.HashSet;
import java.util.Set;

public class JedisDemo1 {

	public static void main(String[] args) {
		JedisDemo1 demo1=new JedisDemo1();
		//demo1.demo1();
		//demo1.demo2();
		demo1.redisCluster();
	}

	/**
	 * 单机
	 */
	public void demo1(){

		Jedis jedis=new Jedis("192.168.37.130",6379);
		jedis.set("zzy", "dsd");
		String value=jedis.get("zzy");
		System.out.println(value);
		jedis.close();
	}

	/**
	 * 单机链接吃
	 */
   public void demo2(){

		JedisPoolConfig config=new JedisPoolConfig();

		config.setMaxTotal(30);

		config.setMaxIdle(10);
		

		JedisPool jedisPool=new JedisPool(config,"192.168.37.130",6379);
		

		Jedis jedis=null;
		try{
			jedis=jedisPool.getResource();
			jedis.set("wxm", "kbd");
			String value=jedis.get("wxm");
			System.out.println(value);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(jedis!=null){
				jedis.close();
			}
			if(jedisPool!=null){
				jedisPool.close();
			}
		}
		
		
	}

	/**
	 * 集群版链接(自带链接吃)
	 */
	public void redisCluster(){
		Set<HostAndPort> nodes= new HashSet();
		nodes.add(new HostAndPort("192.168.37.130",7001));
		nodes.add(new HostAndPort("192.168.37.130",7002));
		nodes.add(new HostAndPort("192.168.37.130",7003));
		nodes.add(new HostAndPort("192.168.37.130",7004));
		nodes.add(new HostAndPort("192.168.37.130",7005));
		nodes.add(new HostAndPort("192.168.37.130",7006));
		GenericObjectPoolConfig poolConfig=new GenericObjectPoolConfig();
		JedisCluster jedisCluster=new JedisCluster(nodes,poolConfig);
		jedisCluster.set("zzy","1231");
		System.out.println(jedisCluster.get("zzy"));
		jedisCluster.close();
	}

} 
